import Server from "@/config/server";
import request from '@/utils/request';
import router from '@/router';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  equipmentDataData: [],
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.agreement_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            // fields.push({
            //     title: '操作',
            //     align: 'center',
            //     width: 120,
            //     scopedSlots: { customRender: 'action' },
            //     fixed: 'right',
            // })
            // fields[fields.length-2].width = 140
            fields.forEach(function (item, i) {
              item.dataIndex = fields[i].key
              item.align = 'center';
              if(i < fields.length-1) {
                fields[i].width = item.width || "8%"
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  dataAdd({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.agreement_add, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        console.log(response);
        showSuccess(response.data.result)
        router.push({
          query: {
            tabsKey: 3,
            equipment_id: page.equipment_id
          }
        })
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  dataEdit({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.agreement_update, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
        router.push({
          query: {
            tabsKey: 3,
            equipment_id: page.equipment_id,
            edit: true,
          }
        })
      } else {
        showError( response.data.message )
      }
    })
  },
  // /* ID删除 > 参数为：url/id */
  dataDel({commit}, id) {
    request.get(Server.action.agreement_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },

  // /* 批量删除 > 参数为：[1,2,3,4,5,6] */
  // dataDel({commit}, ids) {
  //   request.get(Server.action.agreement_delete ,{ params: ids })
  //   .then(response => {
  //     console.log(response);
  //     if (response.data.errorCode === 0) {
  //       showSuccess(response.data.result)
  //     } else {
  //       showError( response.data.message )
  //     }
  //   })
  // },
  // 详情
  dataDetail({commit}, id) {
    request.get(Server.action.agreement_detailEquipment + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDetail", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
	setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
		state.dataLists = lists;
	},
	setFields(state, lists) {
		state.Fields = lists;
	},
  setDetail(state, data) {
      state.itemDetail = data;
  },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
